#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 14-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

using Thn;
#endregion

namespace Thn.Interface.Vcl
{
    /// <summary>
    /// A non-visual prop that blinks another viual control.
    /// </summary>
    //[ToolboxItem(true), DefaultProperty("Target"), DefaultEvent("Tick"), ToolboxBitmap(typeof(ResourceBeacon), "Thn.Interface.Vcl.Resources.Icons.Blinker.bmp")]
    //[ToolboxItem(true), DefaultProperty("Target")]
    [ToolboxItem(true)]
#if DEBUG
    public
#else
    public
#endif
 class Blinker : Component
    {
        #region Fields
        Timer mTimer = null;
        int mCurrentCount = 0;
        int mMaxCount = 0;
        #endregion

        #region Properties

        #region Is Active
        private bool mIsActive;
        /// <summary>
        /// Checks whether animation is active.
        /// </summary>
        public bool IsActive
        {
            get { return mIsActive; }
        }
        #endregion

        #region Target
        private Control mTarget = null;
        /// <summary>
        /// Gets/Sets the target control for blinking.
        /// </summary>
        [Browsable(true), DefaultValue(null), Category("Data"), Description("Target control for blinking")]
        public Control Target
        {
            get { return mTarget; }
            set { mTarget = value; }
        }
        #endregion

        #region Duration
        private int mDuration = 500;
        /// <summary>
        /// Gets/Sets how long the effect lasts (in milliseconds0.
        /// </summary>
        [Browsable(true), DefaultValue(500), Category("Behavior"), Description("How long the effect lasts in milliseconds")]
        public int Duration
        {
            get { return mDuration; }
            set { mDuration = value; }
        }
        #endregion

        #region Interval
        private int mInterval = 100;
        /// <summary>
        /// Gets/Sets the interval of each blink (in milliseconds)
        /// </summary>
        [Browsable(true), DefaultValue(100), Category("Behavior"), Description("Interval of each blink in milliseconds")]
        public int Interval
        {
            get { return mInterval; }
            set { mInterval = value; }
        }
        #endregion

        #endregion

        #region Blink
        /// <summary>
        /// Start blinking effect
        /// </summary>
        public void Blink()
        {
            if (mTarget != null)
            {
                if (!mIsActive)
                {
                    //prepare timer
                    if (mTimer == null)
                    {
                        mTimer = new Timer();
                        mTimer.Tick += new EventHandler(mTimer_Tick);
                    }
                    #region Defensive tracing
                    else
                    {
                        //already created
                    }
                    #endregion

                    mTimer.Interval = mInterval;
                    mMaxCount = mDuration / mInterval;
                    mCurrentCount = 0;
                    mTimer.Start();
                }
                #region Defensive tracing
                else
                {
                    //already active
                }
                #endregion
            }
            else throw new Exception("Requires a target for blinking");
        }
        #endregion

        #region Timer Tick
        void mTimer_Tick(object sender, EventArgs e)
        {
            if (mCurrentCount < mMaxCount)
            {
                #region Animation is still active
                
                mTarget.Visible = !mTarget.Visible;
                mCurrentCount++;
                
                #endregion
            }
            else
            {
                #region Time's up

                mTimer.Stop();
                mTimer.Dispose();
                mTimer = null;
                mTarget.Visible = true;

                #endregion
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public Blinker()
        { }
        #endregion
    }
}
